package com.edu.atams.utils;

import cn.hutool.core.date.DateUtil;
import cn.hutool.http.HttpUtil;
import com.edu.atams.common.dto.NoticeDto;
import com.edu.atams.entity.Notice;
import com.edu.atams.entity.Voting;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class AsyncRequestUtil {
    @Autowired
    WeChatUtil weChatUtil;

    @Async
    public void asyncPostRequest(String url, String body) throws Exception {
        String result = HttpUtil.post(url,body);
        System.out.println("result" +  " --- " + result);
    }

    @Async
    public void sendVotingNotice(List<String> openIds, Voting voting){
        Map<String,String> templateKeywords = new HashMap<>();
        templateKeywords.put("title", voting.getTitle());
        templateKeywords.put("end_time", DateUtil.format(voting.getEndTime(), "yyyy-MM-dd hh:mm"));
        templateKeywords.put("nickname", voting.getPublisherName());
        weChatUtil.sendHomeworkNotice(openIds,templateKeywords);
    }

    @Async
    public void sendHomeworkNotice(List<String> openIds, NoticeDto noticeDto){
        Map<String,String> templateKeywords = new HashMap<>();
        templateKeywords.put("title",noticeDto.getNotice().getTitle());
        templateKeywords.put("end_time", DateUtil.format(DateUtil.date(), "yyyy-MM-dd hh:mm"));
        templateKeywords.put("nickname",noticeDto.getNotice().getNickname());
        weChatUtil.sendHomeworkNotice(openIds,templateKeywords);
    }

    @Async
    public void sendSchoolNotice(List<String> openIds, NoticeDto noticeDto){
        Map<String,String> templateKeywords = new HashMap<>();
        templateKeywords.put("type", noticeDto.getNotice().getType() ==2 ? "校内通知" : "学校公告");
        templateKeywords.put("title", noticeDto.getNotice().getTitle());
        templateKeywords.put("nickname",noticeDto.getNotice().getNickname());
        weChatUtil.sendSchoolNotice(openIds,templateKeywords);
    }
}
